(function () {

    "use strict";

    var api = {
        getDetailUrl: '/api/order/content',
        submitUrl: '/api/order/refund'
    };
    sm.controller('wx-order-refund-controller', ['$scope', '$http', 'Modal', 'Toast', function ($scope, $http, Modal, Toast){

        $scope.goods = [];
        $scope.address = {};
        $scope.message = '';
        $scope.isSubmitting = false;

        var orderId = GetQueryString('orderId');

        // 物流配置
        $scope.getOrder = function () {
            $http.get(api.getDetailUrl, {
                params: {
                    orderId: orderId
                }
            }).success(function (res) {
                if (res.state) {
                    var data = res.data;
                    if (data.address) {
                        $scope.address = data.address;
                    }

                    if (data.goods) {
                        $scope.goods = data.goods;
                    }
                } else {
                    Toast.error({title: '加载失败'});
                }
            });
        };

        $scope.getOrder();

        $scope.commit = function () {

            if (!$scope.message || !orderId) {
                Modal.alert({
                    'title': '操作提示',
                    'content': '含有必填项没有填写',
                    'buttons': [{
                        name: '确定'
                    }]
                });
                return false;
            }

            Modal.alert({
                title: '操作提示',
                content: '您确定申请退款吗?',
                buttons: [{
                    name: '取消',
                    event: function () {}
                }, {
                    name: '立即退款',
                    event: function () {
                        doRefund();
                    }
                }]
            });
        };

        function doRefund() {
            Toast.loading({'title': '正在处理'});
            $scope.isSubmitting = true;
            $http.post(api.submitUrl, {
                orderId: orderId,
                message: $scope.message
            }).success(function (res) {
                Toast.stop();
                $scope.isSubmitting = false;
                if (res.state) {
                    location.href = '/order';
                } else {
                    Toast.error({title: res.error});
                }
            });
        }
    }]);
})();

